﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Xml;

//using System.Net.Dns;

namespace ChatServer.model
{

    public class UserSocket: Socket
    {

        private string _xml;
        private User _user;
        private string _Roomname;
        private string _ipaddress;
        private UserSocket client;
        private IPEndPoint ip;
        private int _userport;

        public UserSocket(string ipaddress):base(new SocketInformation())
        {
            _ipaddress = ipaddress;
            IPAddress addr = IPAddress.Parse(_ipaddress);
            _userport = 8080;
            //setting the socket connection to the client using the IPADDRESS
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, _userport);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Bind(ip);
            socket.Listen(10);

            Socket client = socket.Accept();
            IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
            Console.WriteLine("Connected with {0} at port {1}", newclient.Address, newclient.Port);
        }

        //message sent across usersocket. send the xml in chat object.??????just foward the xml.
        public void sendMessage(Chat Chat)
        {

            string message = _xml;
            Console.WriteLine(_xml);
            //socket method that sends a string or whatever
            this.SendFile(message);
            //work on this
        }

        //internal user property with a getter and setter.
        public User User
        { 
            get {return _user;}
            set { _user = value;}

        }

        //property to expose IP address / required parameter of constructer/just a getter
        public string IpAddress
        {
            get {return _ipaddress;}
        }
    }
}


